package com.sihai.ehr.web.action.duty_proxy;

import com.sihai.core.LimitedList;
import com.sihai.core.beans.BeanInject;
import com.sihai.ehr.model.biz.AuditType;
import com.sihai.ehr.model.biz.DutyProxy;
import com.sihai.ehr.model.biz.DutyProxyAuditing;
import com.sihai.ehr.model.biz.EmployeeinfoBase;
import com.sihai.ehr.service.DutyProxyService;
import com.sihai.ehr.web.action.EHRAction;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import java.util.List;

/**
 * @author: wxn
 * @since: 12-6-16 下午1:49
 * <p/>
 * 职务代理Action
 */

@Results({
        @Result(name = "list", location = "list.jsp"),
        @Result(name = "add", location = "add.jsp"),
        @Result(name = "audit_detail", location = "audit.jsp"),
        @Result(name = "main", location = "main.jsp")
})
public class DutyProxyAction extends EHRAction {
    @BeanInject
    private DutyProxyService dutyProxyService;

    /*set:设置， audit:审核,  list:浏览*/
    private String operation;
    private DutyProxy proxy;
    private LimitedList<DutyProxy> proxies;
    private String ids;

    private int who; //第几代理人

    private List<List<DutyProxyAuditing>> audits;

    private DutyProxyAuditing auditing;

    @Action("/duty_proxy/main")
    @Override
    public String execute() throws Exception {
        listAction = "/duty_proxy/list.xhtml?operation="+operation;
        if (StringUtils.equals(operation, "set")) {
            listTitle = "职务代理人设置";
        } else if (StringUtils.equals(operation, "audit")) {
            listTitle = "职务代理人审核";
        } else if (StringUtils.equals(operation, "list")) {
            listTitle = "职务代理人浏览";
        } else {
            listTitle = "职务代理人设置";
        }

        return "main";
    }

    @Action("/duty_proxy/list")
    public String list() throws Exception {
        proxies = dutyProxyService.findByOrganization(organizationId, createLimit(), createSorter());

        return "list";
    }

    /*跳转到添加、修改页面*/
    @Action("/duty_proxy/add")
    public String add() throws Exception {
        //编辑
        if(proxy != null && proxy.getId() != null && proxy.getId() > 0){
            proxy = dutyProxyService.get(proxy.getId());
        }
        return "add";
    }

    @Action("/duty_proxy/create")
    public String create() throws Exception {
        operation = "set";
        if (proxy == null || proxy.getDuty() == null || proxy.getDuty().getId() == null || proxy.getDuty().getId() == 0) {
            return execute();
        }

        if(proxy.getEmployee() != null && (proxy.getEmployee().getId() == null || proxy.getEmployee().getId() <= 0)){
            proxy.setEmployee(null);
        }
        if(proxy.getProxyOne() != null && (proxy.getProxyOne().getId() == null || proxy.getProxyOne().getId() <= 0)){
            proxy.setProxyOne(null);
        }
        if(proxy.getProxyTwo() != null && (proxy.getProxyTwo().getId() == null || proxy.getProxyTwo().getId() <= 0)){
            proxy.setProxyTwo(null);
        }
        if(proxy.getProxyThree() != null && (proxy.getProxyThree().getId() == null || proxy.getProxyThree().getId() <= 0)){
            proxy.setProxyThree(null);
        }

        //edit
        if(proxy.getId() != null && proxy.getId() > 0){
            DutyProxy dp = dutyProxyService.get(proxy.getId());
            dp.setEmployee(proxy.getEmployee());
            dp.setProxyOne(proxy.getProxyOne());
            dp.setProxyTwo(proxy.getProxyTwo());
            dp.setProxyThree(proxy.getProxyThree());
            dutyProxyService.save(dp);
        }else{//create
            dutyProxyService.save(proxy);
        }

        return execute();
    }

    @Action("/duty_proxy/remove")
    public String remvoe() throws Exception {
        if(StringUtils.isBlank(ids)){
            return execute();
        }
        dutyProxyService.remove(ids);
        
        return execute();
    }

    //跳转到审核详细页面
    @Action("/duty_proxy/audit_detail")
    public String detail() throws Exception {
        if(proxy == null || proxy.getId() == null){
            operation = "audit";
            return execute();
        }

        proxy = dutyProxyService.get(proxy.getId());
        audits = dutyProxyService.findDutyProxyAuditings(proxy.getId(), who);
        if(audits == null || audits.isEmpty()){
            dutyProxyService.saveDutyProxyAuditingProcess(proxy, who);
            audits = dutyProxyService.findDutyProxyAuditings(proxy.getId(), who);
        }
        //检查当前登录用户是否可以审核当前结点
        EmployeeinfoBase base = (who == 1 ? proxy.getProxyOne() : (who == 2 ? proxy.getProxyTwo() : proxy.getProxyThree()));
        if(base != null && audits != null && !audits.isEmpty()){
            checkCanAudit(base, audits.get(audits.size()-1));
        }
        currentAuditType = AuditType.audit_duty_proxy.toString();
        
        return "audit_detail";
    }

    //审核
    @Action("/duty_proxy/audit")
    public String audit() throws Exception {
        if(auditing == null || auditing.getId() == null || auditing.getId() <= 0){
            return detail();
        }

        DutyProxyAuditing da = dutyProxyService.getDutyProxyAuditing(auditing.getId());
        if(da != null){
            da.setAuditContent(auditing.getAuditContent());
            da.setStatus(auditing.getStatus());
            dutyProxyService.saveDutyProxyAuditing(da, who);
        }

        //return detail();
        operation = "audit";
        return execute();
    }

    public String getOperation() {
        return operation;
    }

    public void setOperation(String operation) {
        this.operation = operation;
    }

    public DutyProxy getProxy() {
        return proxy;
    }

    public void setProxy(DutyProxy proxy) {
        this.proxy = proxy;
    }

    public LimitedList<DutyProxy> getProxies() {
        return proxies;
    }

    public void setProxies(LimitedList<DutyProxy> proxies) {
        this.proxies = proxies;
    }

    public String getIds() {
        return ids;
    }

    public void setIds(String ids) {
        this.ids = ids;
    }

    public int getWho() {
        return who;
    }

    public void setWho(int who) {
        this.who = who;
    }

    public List<List<DutyProxyAuditing>> getAudits() {
        return audits;
    }

    public void setAudits(List<List<DutyProxyAuditing>> audits) {
        this.audits = audits;
    }

    public DutyProxyAuditing getAuditing() {
        return auditing;
    }

    public void setAuditing(DutyProxyAuditing auditing) {
        this.auditing = auditing;
    }
}
